package com.zzh.myapplication.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.icarplus.car.R;
import net.icarplus.car.bean.InvoiceAdapterBean;
import net.icarplus.car.views.PinnedHeaderListView;

import java.util.List;

/**
 * Created by Administrator on 2016/4/21.
 */
public class InvoiceCustomAdapter extends BaseAdapter implements
        AbsListView.OnScrollListener, PinnedHeaderListView.PinnedHeaderAdapter {
    public Context context;
    public List<InvoiceAdapterBean> list;
    private static final String TAG = CustomAdapter.class.getSimpleName();

    public InvoiceCustomAdapter(Context context, List<InvoiceAdapterBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {

        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_invoice_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        InvoiceAdapterBean bean = list.get(position);
        viewHolder.title.setText(bean.getTitle());
        viewHolder.title_time.setText(bean.getTitleTime());
        viewHolder.tv_Money.setText(bean.getMoney());
        viewHolder.tv_orderNum.setText(bean.getOrderNum());
        viewHolder.tv_StartAddress.setText(bean.getStartAddress());
        viewHolder.tv_StopAddress.setText(bean.getStopAddress());
        if (needTitle(position)) {
            viewHolder.title.setText(bean.getTitle());
            viewHolder.title_time.setText(bean.getTitleTime());
            viewHolder.title.setVisibility(View.VISIBLE);
            viewHolder.title_time.setVisibility(View.VISIBLE);
        } else {
            viewHolder.title.setVisibility(View.GONE);
            viewHolder.title_time.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHolder {
        ImageView iv_carPic;
        TextView tv_StartAddress;
        TextView tv_StopAddress;
        TextView tv_orderNum;
        TextView tv_Money;
        TextView title;
        TextView title_time;

        public ViewHolder(View view) {
            iv_carPic = (ImageView) view.findViewById(R.id.iv_carPic);
            tv_StartAddress = (TextView) view.findViewById(R.id.tv_StartAddress);
            tv_StopAddress = (TextView) view.findViewById(R.id.tv_StopAddress);
            tv_orderNum = (TextView) view.findViewById(R.id.tv_orderNum);
            tv_Money = (TextView) view.findViewById(R.id.tv_Money);
            title = (TextView) view.findViewById(R.id.title);
            title_time = (TextView) view.findViewById(R.id.title_time);
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {

    }

    private boolean isMove(int position) {
        InvoiceAdapterBean currentEntity = (InvoiceAdapterBean) getItem(position);
        InvoiceAdapterBean nextEntity = (InvoiceAdapterBean) getItem(position + 1);
        if (null == currentEntity || null == nextEntity) {
            return false;
        }
        String currentTitle = currentEntity.getTitle();
        String nextTitle = nextEntity.getTitle();
        if (null == currentTitle || null == nextTitle) {
            return false;
        }
        if (!currentTitle.equals(nextTitle)) {
            return true;
        }
        return false;
    }

    @Override
    public int getPinnedHeaderState(int position) {
        if (getCount() == 0 || position < 0) {
            return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_GONE;
        }
        if (isMove(position) == true) {
            return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP;
        }
        return PinnedHeaderListView.PinnedHeaderAdapter.PINNED_HEADER_VISIBLE;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount) {

        if (view instanceof PinnedHeaderListView) {
            ((PinnedHeaderListView) view).controlPinnedHeader(firstVisibleItem);
        }
    }

    private boolean needTitle(int position) {
        if (position == 0) {
            return true;
        }
        if (position < 0) {
            return false;
        }
        InvoiceAdapterBean currentEntity = (InvoiceAdapterBean) getItem(position);
        InvoiceAdapterBean previousEntity = (InvoiceAdapterBean) getItem(position - 1);
        if (null == currentEntity || null == previousEntity) {
            return false;
        }
        String currentTitle = currentEntity.getTitle();
        String previousTitle = previousEntity.getTitle();
        if (null == previousTitle || null == currentTitle) {
            return false;
        }
        if (currentTitle.equals(previousTitle)) {
            return false;
        }
        return true;
    }

    @Override
    public void configurePinnedHeader(View headerView, int position, int alpaha) {
        InvoiceAdapterBean itemEntity = (InvoiceAdapterBean) getItem(position);
        String headerValue = itemEntity.getTitle();
        if (!TextUtils.isEmpty(headerValue)) {
            TextView headerTextView = (TextView) headerView.findViewById(R.id.header);
            headerTextView.setText(headerValue);
        }
    }
}
